﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.IO;
using System.Collections;
using Newtonsoft.Json.Linq;

namespace BEX.Core.Exchange.CAMPBX
{
    public class CampBX : Exchange
    {
        public CampBX()
            : base(ExchangeType.CAMPBX)
        {
            
        }
   
        internal override Tick GetCurrentTick(CurrencyPair pair)
        {
            CampBXResponse response = GetCampBxTick();

            Tick res = new Tick();


            res.Ask = Convert.ToInt32(Convert.ToDecimal(response.Best_Ask) * global.ConversionFactor);
            res.Bid = Convert.ToInt32(Convert.ToDecimal(response.Best_Bid) * global.ConversionFactor);
            res.Currency = pair;
            res.ExchangeSource = ExchangeType.CAMPBX;
            res.Time = DateTime.UtcNow;
            res.Spot = Convert.ToInt32(Convert.ToDecimal(response.Last_Trade) * global.ConversionFactor);

            return res;
        }

        private CampBXResponse GetCampBxTick()
        {
            var queryStr = string.Format(TickerURL, string.Empty);
            var resp = Query(queryStr);
            return new CampBXResponse(JObject.Parse(resp) as JObject);
        }



        private static string Query(string url)
        {
            var request = WebRequest.Create(url);
            request.Method = "GET";
            if (request == null)
            {
                throw new Exception("Non HTTP WebRequest");
            }
            return new StreamReader(request.GetResponse().GetResponseStream()).ReadToEnd();
        }


        protected override string _baseUrl
        {
            get { return ExchangeConfig.Default.CampBXAPI_BaseURL; }
        }

        protected override string _tickPath
        {
            get { return ExchangeConfig.Default.CampBXAPI_GetTickPath; }
        }

        protected override string _pairs
        {
            get { return ExchangeConfig.Default.CampBX_Pairs; }
        }
    }
}
